package com.mars.rabbitmq.delayPlugins;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author King
 * @create 2020/8/19 14:21
 */
@Slf4j
@Component
public class DelayPluginsProducer {

    @Autowired
    private RabbitTemplate rabbitTemplate;


    /**
     * 订单发送到立即消费队列
     *
     * @param msg
     */
    public void convertAndSend(String msg, Integer delay) {
        MessageProperties mp = new MessageProperties();
        mp.setDelay(delay);
        Message message = new Message((msg.toString() + "order").getBytes(), mp);
        String orderId = "ddp" + System.currentTimeMillis();
        log.info("订单" + orderId + ":支付倒计时" + delay + "ms....");
        CorrelationData correlationData = new CorrelationData(orderId);
        rabbitTemplate.convertAndSend(DelayPluginsRabbitMqConfig.DELAY_EXCHANGE, DelayPluginsRabbitMqConfig.DELAY_ROUTING_KEY, message, correlationData);
    }


}
